package com.yxjme.seekbarlibrary;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class MySeekBar extends View {

    /*view的宽度*/
    private int mViewWidth;
    /*view的高度*/
    private int mViewHeight;
    /*view的中线*/
    private int viewCenterY;
    /*定义绘制背景paint*/
    private Paint bgPaint;
    /*定义bar的高度*/
    private float seekBarHeight;
    /*定义bar圆角的大小*/
    private int seekBarRadius ;
    /*定义绘制滑块的笔*/
    private Paint thumPaint;
    /*定义滑块半径*/
    private int thumRadius = 20;
    /*滑块移动偏移量*/
    private float change_x_left;
    private float change_x_right;
    /*记录上一次划过的偏移量*/
    private float lastThum1MoveX;
    private float lastThum2MoveX;
    /*是否可以滑动*/
    private boolean isCanScrollThum1;
    private boolean isCanScrollThum2;
    /**/
    private float downX  = 0 ;
    private float offset_left = 0.0f;
    private float offset_right = 1.0f ;
    private Paint middlePaint;
    private int middlePaintColor = Color.RED;

    /*支持滑块图片*/
    private Drawable thumDrawable;


    public MySeekBar(Context context) {
        this(context,null);
    }

    public MySeekBar(Context context,  AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MySeekBar(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MySeekBar, 0, defStyleAttr);
        for (int i = 0 ; i<typedArray.getIndexCount();i++){
            int attr = typedArray.getIndex(i);
            if (attr == R.styleable.MySeekBar_seekBarHeight) {
                seekBarHeight = typedArray.getInt(attr, 20);
            } else if (attr == R.styleable.MySeekBar_seekBarRadius) {
                seekBarRadius = typedArray.getInt(attr, 0);
            } else if (attr == R.styleable.MySeekBar_thumRadius) {
                thumRadius = typedArray.getInt(attr, 10);
            } else if (attr == R.styleable.MySeekBar_middlePaintColor) {
                middlePaintColor = typedArray.getColor(attr, Color.RED);
            } else if (attr == R.styleable.MySeekBar_thumDrawable) {
                thumDrawable = typedArray.getDrawable(attr);
            }
        }
        typedArray.recycle();
        init();
    }


    private void init() {
        /*定义绘制背景paint*/
        bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        bgPaint.setColor(Color.GRAY);
        bgPaint.setAntiAlias(true);
        bgPaint.setDither(true);
        bgPaint.setStrokeJoin(Paint.Join.ROUND);
        bgPaint.setStrokeCap(Paint.Cap.ROUND);


        thumPaint = new Paint();
        thumPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        thumPaint.setColor(Color.RED);
        thumPaint.setAntiAlias(true);
        thumPaint.setDither(true);
        thumPaint.setStrokeJoin(Paint.Join.ROUND);
        thumPaint.setStrokeCap(Paint.Cap.ROUND);


        middlePaint = new Paint();
        middlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        middlePaint.setColor(middlePaintColor);
        middlePaint.setAntiAlias(true);
        middlePaint.setDither(true);
        middlePaint.setStrokeJoin(Paint.Join.ROUND);
        middlePaint.setStrokeCap(Paint.Cap.ROUND);
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        leftThum(event);
        rightThum(event);
        invalidate();
        if (onChangerProgressListener!=null){
            onChangerProgressListener.onProgress(Math.min(offset_left,offset_right),Math.max(offset_left,offset_right));
        }
        return true;
    }




    /**
     * 右边滑块
     *
     * @param event
     */
    private void rightThum(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = event.getX() ;
                if (downX >= (mViewWidth+change_x_right-thumRadius*2) && downX <= (mViewWidth+change_x_right)){
                    Log.v("=====downX========","可以滑动");
                    isCanScrollThum2 = true ;
                    isCanScrollThum1 = false ;
                }else {
                    isCanScrollThum2 = false ;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (isCanScrollThum2){
                    change_x_right = event.getX() - downX + lastThum2MoveX;
                    offset_right = (float) (((mViewWidth+change_x_right)*1.0)/mViewWidth);
                    Log.v("=====f====","right="+offset_right);
                    /*左边边界值*/
                    if (mViewWidth + change_x_right-thumRadius*2<=0){
                        change_x_right = thumRadius*2 - mViewWidth ;
                        offset_right=0.0f;
                    }
                    /*右边边界值判断*/
                    if (change_x_right >=0 ){
                        change_x_right = 0;
                        offset_right=1.0f;
                    }
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                lastThum2MoveX = change_x_right;
                break;
        }
    }



    /**
     * 左边滑块
     *
     * @param event
     */
    private void leftThum(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = event.getX() ;
                if (downX >= (change_x_left -thumRadius) && downX<=(change_x_left+thumRadius*2)*2){
                    Log.v("=====downX========","可以滑动");
                    isCanScrollThum1 = true ;
                }else {
                    isCanScrollThum1 = false ;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (isCanScrollThum1){
                    change_x_left = event.getX() - downX + lastThum1MoveX;
                    offset_left = (float) (((change_x_left+thumRadius*2)*1.0)/mViewWidth);

                    /*左边边界值*/
                    if (change_x_left <= 0){
                        change_x_left = 0 ;
                        offset_left = 0.0f ;
                    }



                    /*右边边界值判断*/
                    if (change_x_left>=mViewWidth-thumRadius*2){
                        change_x_left = mViewWidth-thumRadius*2;
                        offset_left = 1.0f ;
                    }
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                lastThum1MoveX = change_x_left;
                break;
        }
    }





    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed){
            mViewWidth = getMeasuredWidth();
            mViewHeight = getMeasuredHeight();
            viewCenterY = mViewHeight / 2 ;


        }
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));
    }



    /**
     * 测量控件的高度
     *
     * @param heightMeasureSpec
     * @return
     */
    private int measureHeight(int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int hSize = MeasureSpec.getSize(heightMeasureSpec);
        int h ;
        if (mode == MeasureSpec.EXACTLY){
            h = hSize ;
        }else {
            if (mode==MeasureSpec.AT_MOST){
                h = (int) Math.max(thumRadius*2,seekBarHeight);
            }else {
                h = 0 ;
            }
        }
        return h ;
    }

    /**
     * 测量宽度
     *
     * @param widthMeasureSpec
     * @return
     */
    private int measureWidth(int widthMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int wSize = MeasureSpec.getSize(widthMeasureSpec);
        int w ;
        if (mode == MeasureSpec.EXACTLY){
            w = wSize ;
        }else {
            if (mode==MeasureSpec.AT_MOST){
                w = (int) Math.max(thumRadius*2,0);
            }else {
                w = 0 ;
            }
        }
        return w ;
    }




    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /*绘制背景*/
        drawBg(canvas);
        /*绘制区间值*/
        drawMiddleBg(canvas);
        /*绘制左边的滑块*/
        drawLeftThum(canvas);
        /*绘制右边滑块*/
        drawRightThum(canvas);
    }


    /**
     * 绘制曲线线段
     *
     * @param canvas
     */
    private void drawMiddleBg(Canvas canvas) {
        RectF rect = new RectF(Math.min(mViewWidth+change_x_right,change_x_left),viewCenterY - seekBarHeight/2,Math.max(mViewWidth+change_x_right,change_x_left),viewCenterY + seekBarHeight/2);
        canvas.drawRoundRect(rect,seekBarRadius,seekBarRadius,middlePaint);
    }


    /**
     * 绘制右边滑块
     * @param canvas
     */
    private void drawRightThum(Canvas canvas) {
        canvas.drawCircle(mViewWidth-thumRadius+change_x_right,viewCenterY,thumRadius,thumPaint);
    }


    /**
     * 绘制左边滑块
     *
     * @param canvas
     */
    private void drawLeftThum(Canvas canvas) {
//        if (thumDrawable!=null){
//            BitmapDrawable bitmapDrawable = (BitmapDrawable) thumDrawable;
//            Bitmap b = bitmapDrawable.getBitmap();
//            canvas.drawBitmap(b,change_x_left,viewCenterY-b.getHeight()/2,thumPaint);
//        }else {
//
//        }

        canvas.drawCircle(thumRadius+change_x_left,viewCenterY,thumRadius,thumPaint);
    }



    /**
     * 绘制背景
     *
     * @param canvas
     */
    private void drawBg(Canvas canvas) {
        RectF rect = new RectF(0,viewCenterY - seekBarHeight/2,mViewWidth,viewCenterY + seekBarHeight/2);
        canvas.drawRoundRect(rect,seekBarRadius,seekBarRadius,bgPaint);
    }


    public interface OnChangerProgressListener{
        void onProgress(float minVal, float maxVal);
    }


    private  OnChangerProgressListener onChangerProgressListener ;
    public void setOnChangerProgressListener(OnChangerProgressListener onChangerProgressListener) {
        this.onChangerProgressListener = onChangerProgressListener;
    }
}
